package CompositePattern.example;

import IteratorPattern.Iterator;

import java.util.ArrayList;
import java.util.ListIterator;

public class Directory extends Entry {
    private String name;
    private ArrayList<Entry> directory = new ArrayList<Entry>();

    public Directory(String name) {
        this.name = name;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public Entry add(Entry entry) throws FileTreatMentException {
        directory.add(entry);
        return this;
    }

    @Override
    public int getSize() {
        int size = 0;
        ListIterator<Entry> it = directory.listIterator();
        while (it.hasNext()) {
            Entry entry = it.next();
            size = size + entry.getSize();
        }
        return size;
    }

    @Override
    protected void printList(String pre_path) {
        System.out.println(pre_path + "/" + this);
        ListIterator<Entry> it = directory.listIterator();
        while (it.hasNext()) {
            Entry entry = it.next();
            entry.printList(pre_path + "/" + name);
        }
    }
}
